/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import datos.ClienteBD;
import datos.FacturaDB;
import datos.ProductoDB;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import negocio.Cliente;
import negocio.Factura;
import negocio.Producto;
import negocio.Venta;

/**
 *
 * @author pablo
 */
public class VentanaFactura extends javax.swing.JInternalFrame {

    Producto producto = new Producto();
    ProductoDB productoBase = new ProductoDB();
    Factura factura = new Factura();
    Validacion validacion = new Validacion();
    Cliente cliente = new Cliente();
    boolean entra;

    /**
     * Creates new form VentanaFactura
     */
    public VentanaFactura() throws SQLException {
        initComponents();
        entra = false;
        String[] columnas = {"N°", "Producto", "Codigo", "Precio", "Cantidad", "SubTotal"};
        DefaultTableModel temp = new DefaultTableModel(new String[][]{}, columnas);
        productos.setModel(temp);
        productos.updateUI();

        Calendar c = new GregorianCalendar();
        lbfecha.setText(c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DATE));

        FacturaDB fb = new FacturaDB();
        String id = null;
        try {
            id = fb.DameId();
        } catch (SQLException ex) {
            Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
        }
        numFactura.setText(id);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        numFactura = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        lbfecha = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtdireccion = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtnombre = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtcedula = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtapellido = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txttelefono = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        productos = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        txtProducto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        nP = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(1000, 576));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel1.setBackground(new java.awt.Color(51, 0, 204));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Factura de Venta");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Nro. Factura");

        numFactura.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        numFactura.setForeground(new java.awt.Color(0, 0, 153));
        numFactura.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        numFactura.setText("0001");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
            .addComponent(numFactura, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(numFactura, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(19, 19, 19))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbfecha.setEnabled(false);

        jLabel4.setText("Fecha de Emicion:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(416, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbfecha)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion:"));

        jLabel5.setText("Cedula:");

        txtdireccion.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtdireccion.setEnabled(false);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jButton1.setText("Limpiar");

        jLabel6.setText("Nombre:");

        txtnombre.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtnombre.setEnabled(false);

        jLabel7.setText("Direccion:");

        txtcedula.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtcedula.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtcedulaCaretUpdate(evt);
            }
        });

        jLabel8.setText("Apellido:");

        txtapellido.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtapellido.setEnabled(false);

        jLabel9.setText("Telefono:");

        txttelefono.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txttelefono.setEnabled(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(txtcedula, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtdireccion)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(txttelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 114, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtcedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel7)
                    .addComponent(txtdireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txttelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        productos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        productos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                productosMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                productosMouseExited(evt);
            }
        });
        jScrollPane1.setViewportView(productos);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel10.setText("Total a Pagar:");

        lbTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lbTotal.setForeground(new java.awt.Color(204, 0, 0));
        lbTotal.setText("$ 0,00");

        jButton2.setText("Guardar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cancelar");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbTotal)
                .addGap(18, 18, 18))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addGap(21, 21, 21))
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(lbTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18))
        );

        jLabel12.setText("Producto");

        txtProducto.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        txtProducto.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtProductoCaretUpdate(evt);
            }
        });
        txtProducto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtProductoKeyPressed(evt);
            }
        });

        jLabel3.setText("N° Productos");

        nP.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        nP.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                nPItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(55, 55, 55)
                .addComponent(jLabel3)
                .addGap(35, 35, 35)
                .addComponent(nP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(nP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean valida(String num) {
        try {
            Integer.parseInt(num);

            return true;
        } catch (Exception e) {
            return false;
        }


    }
    private void txtProductoCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtProductoCaretUpdate

        if (valida(txtProducto.getText())) {
            entra = true;
            try {
                Producto p = productoBase.buscar(Integer.parseInt(txtProducto.getText()));
                if (p != null) {
                    producto.setIdproducto(p.getIdproducto());
                    producto.setNombre(p.getNombre());
                    producto.setCantidad(p.getCantidad());
                    txtProducto.setBackground(Color.GREEN);
                    nP.removeAllItems();

                    nP.addItem("-seleccione-");
                    for (int i = 1; i <= producto.getCantidad(); i++) {
                        nP.addItem(i);
                    }
                    nP.updateUI();


                } else {
                    txtProducto.setBackground(Color.RED);
                }
            } catch (SQLException ex) {
                txtProducto.setBackground(Color.RED);
                Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        // TODO add your handling code here:
    }//GEN-LAST:event_txtProductoCaretUpdate

    private void txtProductoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtProductoKeyPressed


        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                Producto p = productoBase.buscar(Integer.parseInt(txtProducto.getText()));
                if (p.getCantidad() > 0 && p != null) {
                    producto.setIdproducto(p.getIdproducto());
                    producto.setNombre(p.getNombre());
                    producto.setCantidad(p.getCantidad());

                    DefaultTableModel temp = (DefaultTableModel) productos.getModel();
                    Object nuevo[] = {temp.getRowCount() + 1, p.getNombre(), p.getIdproducto(), p.getPrecio(), 1, p.getPrecio()};
                    temp.addRow(nuevo);
                    actualizar();
                    productos.updateUI();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "No existen productos disponibles de este tipo");
                }
            } // TODO add your handling code here:
            catch (SQLException ex) {
                Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        // TODO add your handling code here:
    }//GEN-LAST:event_txtProductoKeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        factura.setIdFactura(numFactura.getText());
        factura.setFecha(lbfecha.getText());
        factura.setCedula(txtcedula.getText());
        factura.grabar();
        ProductoDB pro = new ProductoDB();
        DefaultTableModel tb = (DefaultTableModel) productos.getModel();
        for (int i = 0; i < productos.getRowCount(); i++) {
            Venta venta = new Venta();

            venta.setIdFactura(numFactura.getText());
            venta.setIdProcduto(tb.getValueAt(i, 2).toString());
            venta.setCantidad(tb.getValueAt(i, 4).toString());

            venta.grabar();
            try {
                Producto p = pro.buscar(Integer.parseInt(tb.getValueAt(i, 2).toString()));
                p.setCantidad(p.getCantidad() - Integer.parseInt(tb.getValueAt(i, 4).toString()));
                p.modificar();
                this.setVisible(false);
            } catch (SQLException ex) {
                Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
            }
        }




        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void productosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_productosMouseClicked

        float suma = 0;
        if (productos.getSelectedColumn() == 4) {
            DefaultTableModel aux = new DefaultTableModel();
            aux = (DefaultTableModel) productos.getModel();
            for (int i = 0; i < productos.getRowCount(); i++) {
                aux.setValueAt(Double.toString(
                        Float.parseFloat(aux.getValueAt(i, 3).toString()) * Float.parseFloat(aux.getValueAt(i, 4).toString())), i, 5);
                suma = suma + Float.parseFloat(productos.getValueAt(i, 5).toString());
            }
            lbTotal.setText("$ " + suma);

            productos.setModel(aux);
            productos.updateUI();

        }

        // TODO add your handling code here:
    }//GEN-LAST:event_productosMouseClicked

    private void productosMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_productosMouseExited
//        actualizar();
    }//GEN-LAST:event_productosMouseExited

    private void nPItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_nPItemStateChanged

        if (nP.getSelectedItem() != null) {
            if (!nP.getSelectedItem().equals("-seleccione-") && entra) {
                if (nP.getSelectedIndex() != -1) {
                    entra = false;
                    int n = Integer.parseInt(nP.getSelectedItem().toString());
                    try {
                        Producto p = productoBase.buscar(Integer.parseInt(txtProducto.getText()));
                        if (p.getCantidad() > 0 && p != null) {
                            producto.setIdproducto(p.getIdproducto());
                            producto.setNombre(p.getNombre());
                            producto.setCantidad(p.getCantidad());

                            DefaultTableModel temp = (DefaultTableModel) productos.getModel();
                            Object nuevo[] = {temp.getRowCount() + 1, p.getNombre(), p.getIdproducto(), p.getPrecio(), n, p.getPrecio()};
                            temp.addRow(nuevo);
                            actualizar();
                            productos.updateUI();
                        } else {
                            JOptionPane.showMessageDialog(rootPane, "No existen productos disponibles de este tipo");
                        }
                    } // TODO add your handling code here:
                    catch (SQLException ex) {
                        Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
        // TODO add your hanlidling code here:
    }//GEN-LAST:event_nPItemStateChanged

    private void txtcedulaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtcedulaCaretUpdate
        txtnombre.setText(null);
        txtapellido.setText(null);
        txttelefono.setText(null);

        try {
            ClienteBD clienteBD = new ClienteBD();
            validacion = new Validacion();
            /*this.validacion.validadorDeCedula(txtcedula.getText())*/
            if (txtcedula.getText().length() == 10) {
                if (clienteBD.buscar(txtcedula.getText()) != null) {
                    txtcedula.setBackground(Color.green);
                    cliente = clienteBD.buscar(txtcedula.getText());
                    txtnombre.setText(cliente.getNombre());
                    txtapellido.setText(cliente.getApellido());
                    txttelefono.setText(cliente.getTelefono());
                    txtcedula.setBackground(Color.green);
                    

                }
                else
                {
                    txtcedula.setBackground(Color.red);
                }
            } else {
                txtcedula.setBackground(Color.red);
            }
            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(VentanaFactura.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtcedulaCaretUpdate
    public void actualizar() {
        float suma = 0;


        DefaultTableModel aux = new DefaultTableModel();
        aux = (DefaultTableModel) productos.getModel();
        for (int i = 0; i < productos.getRowCount(); i++) {
            aux.setValueAt(Double.toString(
                    Float.parseFloat(aux.getValueAt(i, 3).toString()) * Float.parseFloat(aux.getValueAt(i, 4).toString())), i, 5);
            suma = suma + Float.parseFloat(productos.getValueAt(i, 5).toString());
        }
        lbTotal.setText("$ " + suma);
        productos.setModel(aux);
        productos.updateUI();


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JTextField lbfecha;
    private javax.swing.JComboBox nP;
    private javax.swing.JLabel numFactura;
    public javax.swing.JTable productos;
    private javax.swing.JTextField txtProducto;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtcedula;
    private javax.swing.JTextField txtdireccion;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txttelefono;
    // End of variables declaration//GEN-END:variables
}
